//
//  SyncFileParseOperation.m
//  BosidengIphone
//
//  Created by  on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SyncFileParseOperation.h"

@implementation SyncFileParseOperation

@synthesize data=_data;

- ( id )initWithPathString:( NSString *) path xmlParser:( BaseXmlParser *) parser delegate:( id )obj {
    
    if ( self = [ super init ]) {
        
        filePath = [path retain];
        
        baseParser =[parser retain];
        
        delegate =obj;
        
        ParseData *d = [[ParseData alloc] init];
        self.data = d;
        [d release];
        
    }
    
    return self ;
    
} 

-( void )dealloc{
    [ _data release ], _data = nil ;
    [ filePath release ];
    [baseParser release];
    [ super dealloc ];
    
} 

-( void )setUserInfo:(NSDictionary *)userInfo {
    
    _data.userInfo = userInfo;
    
} 


#pragma mark NSOperation 的生命周期方法

// 开始线程 - 本类的主方法

- ( void )start {
    
    NSLog ( @"operation start!" );
    
    if (![ self isCancelled ]) {
        
        NSFileManager *fm = [NSFileManager defaultManager];
        BOOL fileExist = [fm fileExistsAtPath:filePath];
        if (!fileExist) {
            _data.status = kParseDataFailed;
            _data.error = [NSError errorWithDomain:@"File not Exist!" code:0 userInfo:nil];
            [ self statusChangedNotify ];
        } else {
            NSData *fileData = [fm contentsAtPath:filePath];
            // 创建 XML 解析器指针
            
            _parserContext = xmlCreatePushParserCtxt (& _saxHandlerStruct , baseParser , NULL , 0 , NULL );
            
            //NSLog ( @"request completed!" );
            
            // 使用 libxml 解析器进行 xml 解析
            
            xmlParseChunk ( _parserContext , ( const char *)[ fileData bytes], [ fileData length ], 0 );
            
            // 添加解析数据（结束），注意最后一个参数 terminate
            
            xmlParseChunk ( _parserContext , NULL , 0 , 1 );
            
            // 添加解析数据（结束），
            
            if ( baseParser != nil ){
                
                _data.data = [[[ baseParser getResult ] copy ] autorelease];
                
            } else {
                
                NSLog ( @"baseparser is nil" );
                
            }
            
            
            
            // 释放 XML 解析器
            
            if ( _parserContext ) {
                
                xmlFreeParserCtxt ( _parserContext ), _parserContext = NULL ;
                
            }
            _data.status = kParseDataFinished;
            [ self statusChangedNotify ];
        }
    }
    
}

// 停止线程

- ( void )cancel

{
    
    [ super cancel ];
    
} 

// status 状态变化通知

-( void )statusChangedNotify{
    
    if ( delegate != nil ) {
        
        SEL sel= NSSelectorFromString ( @"syncRequestParseStatusNofity:" );
        
        if ([ delegate respondsToSelector :sel]){
            
            [ delegate performSelector :sel withObject : _data ]; // 注意冒号说明带 1 个参数
            
        } 
        
    }
    
}
@end
